package com.example.uploadhealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.uploadhealth.domain.ResponseResult;
import com.example.uploadhealth.domain.eneity.User;
import com.example.uploadhealth.domain.vo.PageVo;
import com.example.uploadhealth.enums.AppHttpCodeEnum;
import com.example.uploadhealth.exception.SystemException;
import com.example.uploadhealth.mapper.UserMapper;
import com.example.uploadhealth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult getAllUser(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        //用户类型为1
        queryWrapper.eq(User::getType,1);
        IPage<User> page =new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<User> list1 = page.getRecords();
        PageVo pageVO=new PageVo(list1,page.getTotal());
        return ResponseResult.okResult(pageVO);
    }
    @Override
    public ResponseResult getAllAdmin() {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        //用户类型为0
        queryWrapper.eq(User::getType,0);
        List<User> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }
    @Override
    public ResponseResult register(User user) {
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_IS_NULL);
        }
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_IS_NULL);
        }
        if(Objects.equals(user.getType(),1)){
            if(!StringUtils.hasText(user.getClassName())){
                throw new SystemException(AppHttpCodeEnum.CLASS_IS_NULL);
            }
        }
        if(!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_IS_NULL);
        }
        if(userNameIsExit(user)>0){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        save(user);
        return ResponseResult.okResult("添加成功");
    }
    /*
    删除指定id用户
     */
    @Override
    public ResponseResult deletUser(Long id) {
        if(!Objects.isNull(id)){
            boolean b = removeById(id);
            if(b){
                System.out.println("删除成功"+id);
            }
        }
        System.out.println("删除成功"+id);
//        else{
//            throw new SystemException(AppHttpCodeEnum.IDNOEXIT);
//        }
        return ResponseResult.okResult("删除成功");
    }


    private int userNameIsExit(User user) {
        LambdaQueryWrapper<User> queryWrappe=new LambdaQueryWrapper<>();
        queryWrappe.eq(User::getUserName,user.getUserName());
        int count = count(queryWrappe);
        return count;
    }
}
